//
//  SearchApprovedNewsViewController.m
//  pushNewsManager
//
//  Created by Jobs on 8/6/17.
//  Copyright © 2017 com.Iori. All rights reserved.
//

#import "SearchApprovedNewsViewController.h"
#import "NewsSearchResultViewController.h"
#import "Header.h"
#import "GetChannelListApi.h"
#import "ChannelListEntity.h"

@interface SearchApprovedNewsViewController ()
{
    IoriDatePicker *channelPicker;
}
@property (nonatomic, copy) NSString *channelID;
@property (nonatomic, strong) NSArray<ChannelEntity*> *dataSource;
@end

@implementation SearchApprovedNewsViewController

-(void)addAllChancelToDataSource
{
    if(self.dataSource == nil) self.dataSource = [NSArray array];
    NSMutableArray *array = [NSMutableArray arrayWithArray:self.dataSource];
    ChannelEntity *entity = [ChannelEntity new];
    entity.channel_id = @"0";
    entity.channel_name = @"全部";
    [array insertObject:entity atIndex:0];
    self.dataSource = array;
    [self.btnChannel setTitle:self.dataSource.firstObject.channel_name forState:UIControlStateNormal];
    self.channelID = self.dataSource.firstObject.channel_id;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    GetChannelListApi *api = [GetChannelListApi new];
    api.member_id = [UserInfo sharedUser].admin.admin_id;
    WS(weakSelf);
    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request)
    {
        if([IoriJsonHelper getErrorCode:request] == 100)
        {
            ChannelListEntity *channelListEntity = [ChannelListEntity yy_modelWithJSON:[IoriJsonHelper getDictForKey:@"result" fromDict:request.responseJSONObject]];
            NSMutableArray *tempArray = [NSMutableArray arrayWithArray:channelListEntity.channel_list];
            [tempArray addObjectsFromArray:channelListEntity.my_channel];
            weakSelf.dataSource = tempArray;
        }
        else
        {
            [weakSelf showMessage:[IoriJsonHelper getError:request] ];
        }
        [weakSelf addAllChancelToDataSource];
    } failure:^(YTKBaseRequest *request) {
        [weakSelf showMessage:[request responseString]];
        [weakSelf addAllChancelToDataSource];
    }];;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    if([segue.identifier isEqualToString:@"segueResult"])
    {
        NewsSearchResultViewController *vc = segue.destinationViewController;
        vc.iSearchType = 1;
        vc.stime = [self.btnStartDate titleForState:UIControlStateNormal];
        vc.etime = [self.btnEndDate titleForState:UIControlStateNormal];
        vc.channel_id = self.channelID;
        if(self.radType.selectedButton.tag == 0)
        {
            vc.searchTitleContent = self.txtSearch.text;
        }
        else if(self.radType.selectedButton.tag == 1)
        {
            vc.nickname = self.txtSearch.text;
        }
        else if (self.radType.selectedButton.tag == 2)
        {
            vc.member_id = self.txtSearch.text;
        }
    }
}


-(UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleDefault;
}


-(void)btnSearch_click:(UIButton *)sender
{
    [self performSegueWithIdentifier:@"segueResult" sender:sender];
}

- (IBAction)btnChannel_click:(UIButton *)sender
{
    WS(weakSelf);
    [weakSelf.view endEditing:YES];
    if(!channelPicker)
    {
        channelPicker = [[IoriDatePicker alloc] init];
    }
    NSMutableArray<NSString*> *arrayChannelName = [NSMutableArray arrayWithCapacity:5];
    [self.dataSource enumerateObjectsUsingBlock:^(ChannelEntity * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [arrayChannelName addObject:obj.channel_name];
    }];
    
    NSArray<NSString*> *arrayTitles = arrayChannelName;
    [channelPicker showActionMenusWithTitle:@"选择频道" items:arrayTitles inView:self.view sheetHandler:^(UIActionSheet *sheet, NSInteger buttonIndex) {
        [sender setTitle:arrayTitles[buttonIndex-1] forState:UIControlStateNormal];
        weakSelf.channelID = weakSelf.dataSource[buttonIndex-1].channel_id;
    }];
}
@end
